package com.masz.seataone.controller;

import com.masz.seataone.dao.mapper.TbSeata;
import com.masz.seataone.service.SeataOneService;
import com.masz.seataone.service.SeataOneTccService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author masz
 * @Date 2022/2/12 14:11
 */
@RestController
@RequestMapping("/seata")
public class SeataOneController {

    @Value("${server.port}")
    private String port;

    @Autowired
    SeataOneService seataOneService;

    @Autowired
    SeataOneTccService seataOneTccService;

    @PostMapping("/test")
    public String test(@RequestBody TbSeata s){
        seataOneService.test(s.getV());
        return "seata-one-test:"+port;
    }

    @PostMapping("/ta")
    public String taModel(@RequestBody TbSeata s){
        seataOneService.ta(s.getV());
        return "seata-one-test:"+port;
    }

    /**
     * GlobalTransactional 注解必须要加在controller 层，不能直接加在 service 方法上
     * @param s
     * @return
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    @PostMapping("/tcc")
    public String tcc(@RequestBody TbSeata s){
        seataOneTccService.tcc(s.getV());
        return "seata-one-tcc:"+port;
    }




}
